package cn.sfa.controller.login;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.DateUtil;
import cn.sfa.common.utils.FormValidate;
import cn.sfa.common.utils.MajorKeyGenerate;
import cn.sfa.common.utils.SendMailUtil;
import cn.sfa.entity.users.SaleUserLoginLog;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.login.LoginService;
import cn.sfa.service.manage.NewsService;
import cn.sfa.service.role.RoleService;

/**
 * 
* 类名称：LoginController   
* 类描述： 用户登录处理类
* 创建人：lenovo   
* 创建时间：2012-2-14 下午10:26:32   
* 修改人：lenovo   
* 修改时间：2012-2-14 下午10:26:32   
* 修改备注：    
* @version v1.0
 */
@Controller
@RequestMapping("/login/forget/index")
public class ForgetPwdController{
	Logger log  = LoggerFactory.getLogger(ForgetPwdController.class);
	
	private String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Autowired
	LoginService loginService;
	@Autowired
	NewsService newsService;
	@Autowired
	MajorKeyGenerate keyGenerate;
	@Autowired
	RoleService roleService;
	
	@RequestMapping
	public String forgetPwd()
	{
		
		return "/forget/setMail";
	}
	@RequestMapping("sendValidateCode")
	public String sendValidateCode(HttpServletRequest request,ModelMap map)
	{
		String loginname=request.getParameter("loginName");
		String mail=request.getParameter("email");
		map.put("loginName", loginname);
		map.put("email", mail);
		SaleUsersLogin saleUsers=loginService.getUserByLoginName(loginname);
		
		if(CommonUtil.isEmpty(saleUsers.getUserType()))
		{
			 
				map.put("msg", "此用户不存在!");
				log.info("=========================在saleuserslogin表中不存在记录");
				return "/forget/setMail";
			 
		}
		
		//如果是内勤则不需要登录修改密码
		if("USERTYPE_3".equals(saleUsers.getUserType()) || "USERTYPE_4".equals(saleUsers.getUserType()))
		{
			map.put("msg", "对不起！您不能使用此功能！");
			return "/forget/setMail";
		}
		
		String userStatus=saleUsers.getUserStatus();
		if(userStatus!=null && userStatus.equals("n"))
		{
			map.put("msg", "你的帐号已被禁用!");
			return "alter";
		}
		//判断是否是第一次登录，如果是，则修改密码才能进系统
		List users=loginService.getUser(loginname);
		
		if(users.size()==0)
		{
			map.put("msg", "你还没有登录过，不能使用此功能!");
			return "alter";
		}
		
		return "/forget/setMail";
	}
	/**
	 * @param request
	 * @return
	 */
	@RequestMapping("forgetUpdate")
	public String loginUpdatePwd(HttpServletRequest request,ModelMap map)
	{
		String loginname=request.getParameter("loginname");
		String randCodeRequest=request.getParameter("id");
		SaleUsersLogin saleUsers=loginService.getUserByLoginName(loginname);
		String userStatus=saleUsers.getUserStatus();
		if(userStatus!=null && userStatus.equals("n"))
		{
			map.put("msg", "你的帐号已被禁用!");
			return "alter";
		}
		try
		{
			//查询用户
			SaleUsersLogin saleUser=loginService.getUserByLoginName(loginname);
			String begin=saleUser.getVcol1();//用户创建链接时的字符串日期形式
			Date now=new Date();
			String end=DateUtil.dateToString2(new Date());
			String code=saleUser.getVcol2();//存储在表中的随机生成ＩＤ号
			//判断链接生成时间离现在是否超过２４小时，并且随机生成的ＩＤ号与数据库中相对应
			if(DateUtil.timeOut(begin, end)<=24 && randCodeRequest.equals(code))
			{
				request.getSession().setAttribute("loginname", loginname);
				request.getSession().setAttribute("password", saleUser.getUserPassword());
				
				return "/forget/toUpdatePwd";
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return "/forget/alter";
	}
	
	@RequestMapping("toUpdatePwd")
	public String toUpdatePwd(HttpServletRequest request,ModelMap map)
	{
		String loginname=(String)request.getSession().getAttribute("loginname");
		String userPassword=(String)request.getSession().getAttribute("password");
		String newPassword=request.getParameter("newpassword");
		String newPassword2=request.getParameter("newpassword2");
		map.put("newpassword", newPassword);
		map.put("newpassword2", newPassword2);
		
		//在ＡＤ里进行认证看是否存在
		SaleUsersLogin user=loginService.getUserByLoginName(loginname);
		SaleUsersLogin saleUser=loginService.getUserByLoginName(loginname);
		
		String userStatus=saleUser.getUserStatus();
		if(userStatus!=null && userStatus.equals("n"))
		{
			map.put("msg", "你的帐号已被禁用!");
			return "alter";
		}
		
		//表单验证
		if(CommonUtil.isEmpty(newPassword))
		{
			map.put("password1", "新密码不能为空！");
			return "/forget/toUpdatePwd";
		}else if(CommonUtil.isEmpty(newPassword2))
		{
			map.put("password2", "确认密码不能为空！");
			return "/forget/toUpdatePwd";
		}else if(newPassword.length()<7 || newPassword.length()>14)
		{
			map.put("password1", "新密码长度必须在7到14位");
			return "/forget/toUpdatePwd";
		}else if(!FormValidate.pwdStreng(newPassword))
		{
			map.put("password1", "新密码必须包含字母，数字，特殊字符");
			return "/forget/toUpdatePwd";
		}
		else if(newPassword2.length()<7 || newPassword2.length()>14)
		{
			map.put("password2", "确认密码长度必须在7到14位");
			return "/forget/toUpdatePwd";
		}else if(!newPassword.equals(newPassword2))
		{
			map.put("password2", "两次输入的密码不一致！");
			return "/forget/toUpdatePwd";
		}
		
		return "/forget/toUpdatePwd";
	}
}
